package com.whynot.frame.basic.util;

import java.io.*;

/**
 * 功能说明:会话流读取工具类
 * 
 * @author 刘明
 */
public class SessionStreamUtils {
	private static final int FIVE = 5000;

	/**
	 * 
	 * 功能说明:将对象转换成二进制
	 * @param:  Object
	 * @return: byte[]  
	 * @author:刘明
	 *  
	 */
	public static byte[] objectToByteArray(Object obj) throws IOException {
		if (obj == null) {
			return null;
		}
		ObjectOutputStream os = null;
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream(FIVE);
		os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
		os.flush();
		os.writeObject(obj);
		os.flush();
		byte[] sendBuf = byteStream.toByteArray();
		os.close();
		return sendBuf;
	}

	/**
	 * 
	 * 功能说明:将二进制转换成对象
	 * @param:  byte[] bytes
	 * @return: Object  
	 * @author:刘明
	 *  
	 */
	public static Object byteArrayToObject(byte[] bytes) throws IOException {
		if ((bytes == null) || (bytes.length <= 0)) {
			return null;
		}
		Object obj = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
			ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bis));
			obj = ois.readObject();
			bis.close();
			ois.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return obj;
	}
}